import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import tree from "@/components/TensionAndGrout/tree";
// import TableData from "@/components/public/el-table"; //列表
import TableData from "@/components/public/el-table"; //主页面列表
import actions from "@/actions";
import {
    getConcreteCheckWitnessByFid, //详情
    getConcreteCheckWitnessByAdd,//根据受检单位查询编号接口
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getBaseDataitemDetailByItemCode, //字典项(设计强度等级--Qdlevel)
    getWbsList, //WBS
    saveConcreteCheckWitness, //保存

} from "@/api/request.js";
export default {

    data() {
        return {
            isSave: true,
            primaryInfo: null,
            // 页签切换
            changeBtn: false,
            // 页签切换
            isLock: false, //取样按钮是否显示
            dialogHeight: null, //弹框高度
            // 委托单
            timeYMD: null, //当前日期
            timeYmdHms: null,
            btnShow: true,

            formObj: formJs.formObj,

            formData: {
                fId: "",
                userId: "",  //当前登录人id
                userName: "", //当前登录人姓名
                fEntrustno: "", //委托单编号
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目简称
                fEntrustcompanyid: "", //委托单位id
                fEntrustcompanyname: "", //委托单位
                fEntrustuserid: "", //委托人id
                fEntrustusername: "", //委托人
                fCompanyid: "",//受检单位id
                fCompanyname: "", //受检单位
                fEntrustmobile: "", //委托人联系方式
                fProjectuse: "", //工程名称
                fWitnessusername: "", //见证人
                fWitnessunit: "", //见证单位
                fWitnessmobile: "", //见证人联系方式
                fChecktype: "", //检测类别
                fProjecttypename: "", //现场桩号或结构部位
                fProjecttypeid: "", //现场桩号或结构部位id
                fWitnessndates: "", //委托日期
                fSpecimenage: "", //龄期
                fConcretelevel: "",//设计强度等级(MPa)*
                fConcretetype: "", //是否为泵送混凝土
                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fTestnote1: [], //检测依据
                fJudgetype1: [], //判定依据
                fTestresult: "", //检测参数
                fTestprice: "", //检测费用
                fNote: "" //附加说明
            },
            selected: [],
            loading: false,

            // 委托单

            // wbs树
            WbsRow: null,
            dialogEditVisible_wbs: false,
            loadingEditDialog_wbs: false,
            tableData_Wbs: [],
            // wbs树

            dialogHeight: null, //弹框高度
            userInfo: {}, //登录用户
        };
    },
    components: {
        publicForm,
        FormLyout,
        tree,
        TableData,
    },
    created() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded-reboundStrengthManage_reboundEntrust"));
        // console.log("this.primaryInfo", this.primaryInfo);
        this.getHeight();
        this.getData();
        this.getCheckitemsList("3"); //回弹试验依据
        this.getCheckitemsList("4"); //回弹判定依据
    },
    destroyed() {
        window.sessionStorage.removeItem("GxEmbedded-reboundStrengthManage_reboundEntrust");
    },
    mounted() {
        actions.onGlobalStateChange((state, prev) => {
            this.userInfo = prev.currentUser;
        }, true);
        this.formObj.map(item => {
            item.disabled = false;
        });
        this.formObj[0].disabled = true;
        this.formObj[1].disabled = true;
        this.formObj[2].disabled = true;
        this.formObj[5].disabled = true;
        this.isSave = true;
        if (this.primaryInfo.status == 1) { //新增
            this.getBaseTree();
            this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
            this.getEntrustno(this.primaryInfo.getList.fCompanyid, "");

        } else if (this.primaryInfo.status == 2) { //2编辑
            this.getBaseTree();
            this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
        } else if (this.primaryInfo.status == 7) { //查看
            this.formObj.map(item => {
                item.disabled = true;
            })
            this.isSave = false;
            this.getDetail({
                fId: this.primaryInfo.data.fId
            });
        }

    },
    methods: {

        // 页签切换
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(value, index, prop) {
            console.log("selectChange", value, index, prop);
            // 受检单位
            if (b == "fCompanyname") {
                this.formObj[3].options.map(item => {
                    if (data == item.label) {
                        this.formData.fCompanyname = item.label;
                        this.formData.fCompanyid = item.value;
                        this.getEntrustno(this.formData.fCompanyid, this.formData.fWitnessndates);
                    }
                })
            }
            // 委托日期
            if (b == "fWitnessndates") {
                if (this.formData.fCompanyid != "" && this.formData.fCompanyid != null) {
                    this.getEntrustno(this.formData.fCompanyid, this.formData.fWitnessndates);
                }
            }
            // 检测依据
            if (b == 'fTestnote1') {
                var arr = []
                data.map(item => {
                    if (item.label) {
                        arr.push(item.label)
                    } else {
                        arr.push(item)
                    }
                })
                this.formData.fTestnote = arr.join(",");
            }
            // 判定依据
            if (b == 'fJudgetype1') {
                var arr1 = []
                data.map(item => {
                    if (item.label) {
                        arr1.push(item.label)
                    } else {
                        arr1.push(item)
                    }

                })
                this.formData.fJudgetype = arr1.join(",")
            }
        },
        inputFocus(index, prop) {
            console.log("inputFocus", index, prop);
            return index, prop;
        },
        clickDialog(index, prop) {
            console.log("clickDialog", index, prop);
            // 调WBS
            if (prop == "fProjecttypename") {
                this.getToken();
            }
            return index, prop;
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getConcreteCheckWitnessByFid(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.formData = res.data.lrCheckConcreteWitness;
                        // 多选
                        //检测依据
                        if (res.data.lrCheckConcreteWitness.fTestnote != '') {
                            var fTestnote = [];
                            that.formObj[15].options.map(item => {
                                res.data.lrCheckConcreteWitness.fTestnote.split(",").map(i => {
                                    if (item.label == i) {
                                        fTestnote.push(item);
                                    }
                                })
                            })
                        } else {
                            fTestnote = [];
                        }
                        //检测依据
                        //判定依据
                        if (res.data.lrCheckConcreteWitness.fJudgetype != '') {
                            var fJudgetype = [];
                            that.formObj[16].options.map(item => {
                                res.data.lrCheckConcreteWitness.fJudgetype.split(",").map(i => {
                                    if (item.label == i) {
                                        fJudgetype.push(item);
                                    }
                                })
                            })
                        } else {
                            fJudgetype = [];
                        }
                        //判定依据
                        that.selected = [{
                            name: "fTestnote1",
                            selected: fTestnote,
                        },
                        {
                            name: "fJudgetype1",
                            selected: fJudgetype,
                        },
                        ];
                        // 多选

                        if (that.primaryInfo.status == 2) { // 编辑
                            that.formData.userId = that.userInfo.id;  //当前登录人id
                            that.formData.userName = that.userInfo.userName;  //当前登录人姓名
                        }
                    } else {
                        this.loading = false;
                        this.$message.error({
                            content: "取样详情接口报错：" + res.msg,
                            duration: 1000
                        })
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("取样详情接口报错");

                });
        },


        yj_saveForm() {
            if (this.$refs.publicForm.submitForm()) {
                var data = {
                    fId: this.formData.fId,
                    userId: this.userInfo.id,  //当前登录人id
                    userName: this.userInfo.userName, //当前登录人姓名
                    fEntrustno: this.formData.fEntrustno, //委托单编号
                    fProjectid: this.formData.fProjectid, //项目id 隐藏
                    fProjectname: this.formData.fProjectname, //项目简称
                    fEntrustcompanyid: this.formData.fEntrustcompanyid, //委托单位id
                    fEntrustcompanyname: this.formData.fEntrustcompanyname, //委托单位
                    fEntrustuserid: this.formData.fEntrustuserid,//委托人id
                    fEntrustusername: this.formData.fEntrustusername, //委托人
                    fCompanyid: this.formData.fCompanyid,//受检单位id
                    fCompanyname: this.formData.fCompanyname, //受检单位
                    fEntrustmobile: this.formData.fEntrustmobile, //委托人联系方式
                    fProjectuse: this.formData.fProjectuse,//工程名称
                    fWitnessusername: this.formData.fWitnessusername, //见证人
                    fWitnessunit: this.formData.fWitnessunit, //见证单位
                    fWitnessmobile: this.formData.fWitnessmobile,//见证人联系方式
                    fChecktype: this.formData.fChecktype,//检测类别
                    fProjecttypename: this.formData.fProjecttypename, //现场桩号或结构部位
                    fProjecttypeid: this.formData.fProjecttypeid,//现场桩号或结构部位id
                    fWitnessndates: this.formData.fWitnessndates,//委托日期
                    fSpecimenage: this.formData.fSpecimenage, //龄期
                    fConcretelevel: this.formData.fConcretelevel,//设计强度等级(MPa)*
                    fConcretetype: this.formData.fConcretetype, //是否为泵送混凝土
                    fTestnote: this.formData.fTestnote, //检测依据
                    fJudgetype: this.formData.fJudgetype,//判定依据
                    fTestresult: this.formData.fTestresult, //检测参数
                    fTestprice: this.formData.fTestprice, //检测费用
                    fNote: this.formData.fNote, //附加说明
                };
                this.saveForm(data);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true;
            saveConcreteCheckWitness(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;
                        this.$router.push({
                            path: "/reboundStrengthManage/reboundEntrust",
                        });
                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error({
                            content: "保存接口报" + res.errcode,
                            duration: 1000
                        });
                    }
                })
        },

        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },

        //获取委托单编号
        getEntrustno(id, time) {
            var data = {
                fCompanyid: id,//受检单位
                fWitnessndates: time, //委托时间  不传默认当前日期   
            }
            getConcreteCheckWitnessByAdd(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.lrCheckConcretesamplewitness;
                        console.log("获取委托单编号", data);
                        if (time == "") {
                            this.formData = {
                                fId: "",
                                userId: this.userInfo.id,  //当前登录人id
                                userName: this.userInfo.userName, //当前登录人姓名
                                fEntrustno: data.fEntrustno, //委托单编号
                                fProjectid: data.fProjectid,  //项目id 隐藏
                                fProjectname: data.fProjectname, //项目简称
                                fEntrustcompanyid: data.fEntrustcompanyid, //委托单位id
                                fEntrustcompanyname: data.fEntrustcompanyname, //委托单位
                                fEntrustuserid: this.userInfo.id, //委托人id
                                fEntrustusername: this.userInfo.userName, //委托人
                                fCompanyid: data.fCompanyid, //受检单位id
                                fCompanyname: data.fCompanyname, //受检单位
                                fEntrustmobile: "", //委托人联系方式
                                fProjectuse: data.fProjectuse, //工程名称
                                fWitnessusername: "", //见证人
                                fWitnessunit: "", //见证单位
                                fWitnessmobile: "", //见证人联系方式
                                fChecktype: "", //检测类别
                                fProjecttypename: "", //现场桩号或结构部位
                                fProjecttypeid: "", //现场桩号或结构部位id
                                fWitnessndates: this.timeYMD, //委托日期
                                fSpecimenage: "", //龄期
                                fConcretelevel: "",//设计强度等级(MPa)*
                                fConcretetype: "", //是否为泵送混凝土
                                fTestnote: "", //检测依据
                                fJudgetype: "", //判定依据
                                fTestnote1: [], //检测依据
                                fJudgetype1: [], //判定依据
                                fTestresult: "", //检测参数
                                fTestprice: "", //检测费用
                                fNote: "" //附加说明
                            };
                        } else {
                            this.formData.fEntrustno = data.fEntrustno;
                        }

                    } else {
                        this.$message.error({
                            content: "获取委托单编号接口：" + res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        if (num == 3) { //检测依据
                            this.formObj[15].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[15].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        } else if (num == 4) { //判定依据
                            this.formObj[16].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[16].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })
                        }
                    } else {
                        this.$message.error({
                            content: "获取试验依据，检测依据接口：" + res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 查单位
        getBaseTree() {
            var data = {
                companyId: this.primaryInfo.getList.fCompanyid,
                userId: this.userInfo.id
            }
            this.formObj[3].options = [];
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;

                        data.map(item => {
                            this.formObj[3].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })

                    } else {
                        this.$message.error({
                            content: "查单位接口：" + res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                this.formObj[13].options = [];
                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                this.formObj[13].options = arr
            })
        },
        // WBS
        async getToken() {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/roadApi/getToken.do"
            ).then((res) => {
                if (res.status == 200) {
                    sessionStorage.setItem("GxBack-token", res.data.data.Authorizations);
                    this.getWbsDataFun(this.formData.fCompanyid, "");
                }
            });
        },
        getWbsDataFun(id, num) {
            this.dialogEditVisible_wbs = true;
            this.loadingEditDialog_wbs = true;
            getWbsList({
                fCompanyid: id, //云检的标段Id
                projectId: this.primaryInfo.getList.fprojectid, //项目id 必填
                parentId: "-1" //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.tableData_Wbs = res.data.data;
                        this.loadingEditDialog_wbs = false;
                    } else {

                        this.$message.error({
                            content: "wbs数据获取失败" + res.msg,
                            duration: 1000
                        })
                        this.loadingEditDialog_wbs = false;
                    }
                })
                .catch((error) => {
                    console.log("WBS接口报错", error.msg);
                    this.loadingEditDialog_wbs = false;
                });
        },

        treeLoad(tree, treeNode, resolve) {
            getWbsList({
                fCompanyid: tree.fCompanyid, //云检的标段Id
                projectId: this.primaryInfo.getList.fprojectid, //项目id 必填
                parentId: tree.fId //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        return resolve(res.data.data);
                    } else {
                        this.$message.error({
                            content: "wbs数据获取失败" + res.msg,
                            duration: 1000
                        })
                    }
                })
                .catch((error) => {
                    console.log("WBS接口报错", error.msg);
                    this.loading = false;
                });
        },
        // 点击当前行变色
        handleCurrentChange(val) {
            this.WbsRow = val;
        },
        // 行颜色
        tableRowClassName({
            row
        }) {
            if (
                (row.fParentid == 0 && !row.children) ||
                (row.fParentid == null && !row.children)
            ) {
                return "fristColor";
            }
        },
        saveFrom() {
            if (this.WbsRow != null) {
                if (this.WbsRow.fLongname != "" && this.WbsRow.fLongname != null) {
                    this.formData.fProjectlocation = this.WbsRow.fLongname;
                } else {
                    this.formData.fProjectlocation = this.WbsRow.fName;
                }
                this.formData.fProjectlocationid = this.WbsRow.fNodeid;
                this.dialogEditVisible_wbs = false;
            }
        },
        deleFrom() {
            this.dialogEditVisible_wbs = false;
            this.WbsRow = null;
        },
        // WBS
    },
};